package com.kingdee.pda.plugins.http.serverinterface.impl;

import android.text.TextUtils;

import com.example.basecommon.utils.LoginUtil;
import com.kingdee.pda.plugins.http.common.KeyConstants;
import com.kingdee.pda.plugins.http.common.OnRequestCallback;
import com.kingdee.pda.plugins.http.serverinterface.IQueryLocationStock;
import com.kingdee.pda.util.CommonUtil;
import com.example.basecommon.utils.ConstantUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 库位库存查询
 * Created by zhanlg
 * DATE 2021/5/25
 * TIME 17:27
 */
public class QueryLocationStockImpl implements IQueryLocationStock {

    public static final String API_NAME = "/kapi/app/oewms/query_app_pda_location_stock";

    @Override
    public void queryLocationStock(long warehouseId, String barcode, final OnRequestCallback<JSONObject> callback) {
        String apiUrl = ConstantUtil.getServerUrl() + API_NAME;
        HttpUrl.Builder httpBuilder = HttpUrl.parse(apiUrl).newBuilder();
        httpBuilder.addQueryParameter("warehouseId", String.valueOf(warehouseId));
        httpBuilder.addQueryParameter("itemBarcode", barcode);

        Request request = new Request.Builder()
                .addHeader("Content-Type", "application/json; charset=utf-8")
                .addHeader("accessToken", LoginUtil.getToken())
                .url(httpBuilder.build())
                .get()
                .build();
        OkHttpUtils.getInstance().getOkHttpClient().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                callback.onFailedCallback(false, CommonUtil.getErrorMsg(e.getMessage(), "请求失败！"));
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.code() == 200) {
                    String result = response.body().string();
                    if (!TextUtils.isEmpty(result) && result.contains(KeyConstants.Response.SUCCESS)) {
                        JSONObject resultJson;
                        try{
                            resultJson = new JSONObject(result);
                            boolean isSuccess = resultJson.optBoolean(KeyConstants.Response.SUCCESS, false);
                            if (isSuccess) {
                                JSONObject data = resultJson.getJSONObject(KeyConstants.Response.DATA);
                                if (data != null && data.length() > 0) {
                                    callback.onSuccessCallback(data, "");
                                } else {
                                    callback.onFailedCallback(false, "信息获取失败");
                                }
                            } else {
                                callback.onFailedCallback(false,
                                        CommonUtil.getErrorMsg(resultJson.optString(KeyConstants.Response.MESSAGE), "请求失败！"));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            callback.onFailedCallback(false, CommonUtil.getErrorMsg(response.message(), "数据解析失败！"));
                        }
                    } else {
                        callback.onFailedCallback(true, CommonUtil.getErrorMsg(response.message(), "请求失败！"));
                    }
                } else {
                    callback.onFailedCallback(false, response.message());
                }
            }
        });
    }
}
